<?php

/**
 * 行为分析-埋点-主协议
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActMdMainController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_mainPactModel = new Application_Model_ActMdMainPact();
        $this->_mainPactEventModel = new Application_Model_ActMdMainPactEvent();
        $this->_mainPactEventFieldModel = new Application_Model_ActMdMainPactEventField();
        $this->_mainPactIdDefineModel = new Application_Model_ActMdMainPactIdDefine();
        $this->_fieldTypeModel = new Application_Model_ActMdFieldType();
        $this->_mainPactEventFieldEnumModel = new Application_Model_ActMdMainPactEventFieldEnum();
        $this->_mainPactEventFieldLoopModel = new Application_Model_ActMdMainPactEventFieldLoop();
        $this->_childPactModel = new Application_Model_ActMdChildPact();
        $this->_childPactEventFieldEnumModel = new Application_Model_ActMdChildPactEventFieldEnum();
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        //获取当前页码
        $pageSize = 30;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $urls = $vars;

        $orderBy = '`addTime` DESC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_mainPactModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
    /**
     * 添加动作
     * 
     * @return void
     */
    public function mainInsertAction()
    {
        $this->view->pactAuthor= Application_Model_UserLogin::getInstance()->getLoginedUserName();
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['pactStatus'] = trim($this->_request->getPost('pactStatus'));
            $field['keepStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepStartEventId')));
            $field['keepEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepEndEventId')));
            $field['userDefineStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineStartEventId')));
            $field['userDefineEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineEndEventId')));
            $field['pactOnlineDate'] = $filter->filter(trim($this->_request->getPost('pactOnlineDate')));
            $field['addTime']= time();
            $field['pactNote'] = $filter->filter(trim($this->_request->getPost('pactNote')));
            $field['pactAuthor'] = $filter->filter(trim($this->_request->getPost('pactAuthor')));

            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('请输入主协议版本。') ;
                exit;
            }
            if ($this->_mainPactModel->isExists($field['mainVersion'])) {
                echo $this->view->message('主协议版本已经存在，请重新填写。') ;
                exit; 
            }
            $id = $this->_mainPactModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
    }
    
/**
     * 复制动作
     * 
     * @return void
     */
    public function mainCopyAction()
    {
        $curUser = Application_Model_UserLogin::getInstance()->getLoginedUserName();;
        $this->view->pactAuthor= $curUser;
        //主协议版本
        $mainRows = $this->_mainPactModel->fetchAll("`pactStatus`=1");
        $this->view->mainRows = $mainRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $copyMainVersion = $filter->filter(trim($this->_request->getPost('copyMainVersion')));
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['pactStatus'] = trim($this->_request->getPost('pactStatus'));
            $field['keepStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepStartEventId')));
            $field['keepEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepEndEventId')));
            $field['userDefineStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineStartEventId')));
            $field['userDefineEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineEndEventId')));
            $field['pactOnlineDate'] = $filter->filter(trim($this->_request->getPost('pactOnlineDate')));
            $field['addTime']= time();
            $field['pactNote'] = $filter->filter(trim($this->_request->getPost('pactNote')));
            $field['pactAuthor'] = $filter->filter(trim($this->_request->getPost('pactAuthor')));

            $mainRow = $this->_mainPactModel->fetchRow(" `mainVersion`='{$copyMainVersion}'");
            if (!$mainRow) {
                echo $this->view->message('复制协议版本有误，无法进行本次操作。') ;
                exit;
            }
            
            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('请输入主协议版本。') ;
                exit;
            }
            if ($this->_mainPactModel->isExists($field['mainVersion'])) {
                echo $this->view->message('主协议版本已经存在，请重新填写。') ;
                exit; 
            }
            $id = $this->_mainPactModel->insert($field);
            if($id>0){
                //添加事件
                $mainEventRows = $this->_mainPactEventModel->fetchAll("`mainVersion`='{$copyMainVersion}'");
                foreach ($mainEventRows as $row){
                    $doArray = array();
                    $doArray['mainVersion'] = $field['mainVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['eventName'] = $row['eventName'];
                    $doArray['eventBagName'] = $row['eventBagName'];
                    $doArray['eventComplyMode'] = $row['eventComplyMode'];
                    $doArray['eventUploadMode'] = $row['eventUploadMode'];
                    $doArray['eventPoint'] = $row['eventPoint'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_mainPactEventModel->getTableName()}` WHERE `mainVersion`='{$doArray['mainVersion']}' AND `eventId`={$doArray['eventId']}";
                    $row = $this->_mainPactEventModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_mainPactEventModel->insert($doArray);
                    }
                }
                //添加字段
                $mainEventFieldRows = $this->_mainPactEventFieldModel->fetchAll("`mainVersion`='{$copyMainVersion}'");
                foreach ($mainEventFieldRows as $row){
                    $doArray = array();
                    $doArray['mainVersion'] = $field['mainVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_mainPactEventFieldModel->getTableName()}` WHERE `mainVersion`='{$doArray['mainVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort` ={$doArray['fieldSort']}";
                    $row = $this->_mainPactEventFieldModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_mainPactEventFieldModel->insert($doArray);
                    }
                }
                //添加Id定义
                $mainIdDefineRows = $this->_mainPactIdDefineModel->fetchAll("`mainVersion`='{$copyMainVersion}'");
                foreach ($mainIdDefineRows as $row){
                    $doArray = array();
                    $doArray['mainVersion'] = $field['mainVersion'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['pageId'] = $row['pageId'];
                    $doArray['pageName'] = $row['pageName'];
                    $doArray['relationId'] = $row['relationId'];
                    $doArray['aidNote'] = $row['aidNote'];
                    $doArray['isUse'] = $row['isUse'];
                    $doArray['isUseBi'] = $row['isUseBi'];
                    $doArray['isFlag'] = $row['isFlag'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_mainPactIdDefineModel->getTableName()}` WHERE `mainVersion`='{$doArray['mainVersion']}' AND `fieldType`='{$doArray['fieldType']}' AND `pageId`={$doArray['pageId']}";
                    $row = $this->_mainPactIdDefineModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_mainPactIdDefineModel->insert($doArray);
                    }
                }
                //添加枚举类型
                $mainFieldEnumRows = $this->_mainPactEventFieldEnumModel->fetchAll("`mainVersion`='{$copyMainVersion}'");
                foreach ($mainFieldEnumRows as $row){
                    $doArray = array();
                    $doArray['mainVersion'] = $field['mainVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['enum'] = $row['enum'];
                    $doArray['enumValue'] = $row['enumValue'];
                    $doArray['enumAuthor'] = $curUser;
                    $doArray['addTime']= time();
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_mainPactEventFieldEnumModel->getTableName()}` WHERE `mainVersion`='{$doArray['mainVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `enum`={$doArray['enum']}";
                    $row = $this->_mainPactEventFieldEnumModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_mainPactEventFieldEnumModel->insert($doArray);
                    }
                }
                //添加循环类型
                $mainFieldLoopRows = $this->_mainPactEventFieldLoopModel->fetchAll("`mainVersion`='{$copyMainVersion}'");
                foreach ($mainFieldLoopRows as $row){
                    $doArray = array();
                    $doArray['mainVersion'] = $field['mainVersion'];
                    $doArray['eventId'] = $row['eventId'];
                    $doArray['fieldSort'] = $row['fieldSort'];
                    $doArray['loopFieldSort'] = $row['loopFieldSort'];
                    $doArray['fieldName'] = $row['fieldName'];
                    $doArray['fieldType'] = $row['fieldType'];
                    $doArray['complyMode'] = $row['complyMode'];
                    $doArray['addTime']= time();
                    $doArray['note'] = $row['note'];
                    $doArray['remark'] = $row['remark'];
                    
                    $sql = "SELECT count(*) AS c FROM `{$this->_mainPactEventFieldLoopModel->getTableName()}` WHERE `mainVersion`='{$doArray['mainVersion']}' AND `eventId`={$doArray['eventId']} AND `fieldSort`={$doArray['fieldSort']} AND `loopFieldSort`={$doArray['loopFieldSort']}";
                    $row = $this->_mainPactEventFieldLoopModel->getAdapter()->fetchRow($sql);
                    if ($row['c'] == 0){
                        $this->_mainPactEventFieldLoopModel->insert($doArray);
                    }
                }
                
                
                
            
            }

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
    }
    
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function mainUpdateAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['pactStatus'] = trim($this->_request->getPost('pactStatus'));
            $field['keepStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepStartEventId')));
            $field['keepEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('keepEndEventId')));
            $field['userDefineStartEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineStartEventId')));
            $field['userDefineEndEventId'] = (int)$filter->filter(trim($this->_request->getPost('userDefineEndEventId')));
            $field['pactOnlineDate'] = $filter->filter(trim($this->_request->getPost('pactOnlineDate')));
            $field['pactNote'] = $filter->filter(trim($this->_request->getPost('pactNote')));

            if (strlen($field['mainVersion']) ==0) {
                echo $this->view->message('请输入主协议版本。') ;
                exit;
            }
            if ($this->_mainPactModel->isExists($field['mainVersion'], $row['id'])) {
                echo $this->view->message('主协议版本已经存在，请重新填写。') ;
                exit; 
            }
            //修改
            $this->_mainPactModel->update($field, "`id` = {$id}");
            $passwordMsg = ",主版本{$field['mainVersion']}信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }

    /**
     * 删除动作
     * 
     * @return void
     */
    public function mainDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //清空字段
            foreach ($ids as $id){
                $row = $this->_mainPactModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_mainPactEventModel->delete(" `mainVersion`='{$row['mainVersion']}'");
                    $this->_mainPactEventFieldModel->delete(" `mainVersion`='{$row['mainVersion']}'");
                    $this->_mainPactIdDefineModel->delete(" `mainVersion`='{$row['mainVersion']}'");
                    $this->_mainPactEventFieldEnumModel->delete(" `mainVersion`='{$row['mainVersion']}'");
                    $this->_mainPactEventFieldLoopModel->delete(" `mainVersion`='{$row['mainVersion']}'");
                }
            }
            //删除事件
            if (is_array($ids)) {
                $this->_mainPactModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getMainPactVersionIsExistsByJsonAction()
    {
        $account = trim($this->_request->get('account'));
        $mainPactId = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactModel->isExists($account, $mainPactId);
        }
        exit(Zend_Json::encode($isExists));
    }
    
  
    /**
     * 主协议事件列表
     * 
     * @return void
     */
    public function mainEventAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));

        $urls = $vars;
        if ($vars['mainVersion'] != '') {
            $wheres[] = "`mainVersion`= '{$vars['mainVersion']}'";
        }
        $orderBy = '`eventId` ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_mainPactEventModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        
    }
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getMainPactEventIdIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $account = trim($this->_request->get('account'));
        $mainPactEventId = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactEventModel->isExists($mainVersion,$account, $mainPactEventId);
        }
        exit(Zend_Json::encode($isExists));
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getMainPactEventBagNameIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $account = trim($this->_request->get('account'));
        $mainPactEventId = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactEventModel->isExistsByEventBagName($mainVersion,$account, $mainPactEventId);
        }
        exit(Zend_Json::encode($isExists));
    }
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function mainEventInsertAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['eventName'] = $filter->filter(trim($this->_request->getPost('eventName')));
            $field['eventBagName'] = $filter->filter(trim($this->_request->getPost('eventBagName')));
            $field['eventComplyMode'] = (int)$filter->filter(trim($this->_request->getPost('eventComplyMode')));
            $field['eventUploadMode'] = (int)$filter->filter(trim($this->_request->getPost('eventUploadMode')));
            $field['eventPoint'] = $filter->filter(trim($this->_request->getPost('eventPoint')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['eventId']) ==0) {
                echo $this->view->message('请输入事件Id。') ;
                exit;
            }
            $mainPactRow = $this->_mainPactModel->fetchRow("`mainVersion`='{$field['mainVersion']}'");
            $keepStartEventId = isset($mainPactRow['keepStartEventId'])? intval($mainPactRow['keepStartEventId']):0;
            $keepEndEventId = isset($mainPactRow['keepEndEventId'])? intval($mainPactRow['keepEndEventId']):0;
            if (!($field['eventId']>=$keepStartEventId&&$field['eventId']<=$keepEndEventId)) {
                echo $this->view->message('事件Id不在定义区间内,请重新填写。') ;
                exit;
            }
            
            if ($this->_mainPactEventModel->isExists($field['mainVersion'],$field['eventId'])) {
                echo $this->view->message('此协议版本事件Id已经存在，请重新填写。') ;
                exit; 
            }
            
            if (strlen($field['eventBagName']) ==0) {
                echo $this->view->message('请输入事件别名。') ;
                exit;
            }
            if ($this->_mainPactEventModel->isExistsByEventBagName($field['mainVersion'],$field['eventBagName'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件别名[{$field['eventBagName']}]已经存在，请重新填写。") ;
                exit; 
            }
            
            
            $id = $this->_mainPactEventModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'main-event','mainVersion' => $field['mainVersion']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function mainEventUpdateAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactEventModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['eventName'] = $filter->filter(trim($this->_request->getPost('eventName')));
            $field['eventBagName'] = $filter->filter(trim($this->_request->getPost('eventBagName')));
            $field['eventComplyMode'] = (int)$filter->filter(trim($this->_request->getPost('eventComplyMode')));
            $field['eventUploadMode'] = (int)$filter->filter(trim($this->_request->getPost('eventUploadMode')));
            $field['eventPoint'] = $filter->filter(trim($this->_request->getPost('eventPoint')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['eventId']) ==0) {
                echo $this->view->message('请输入事件Id。') ;
                exit;
            }
            $mainPactRow = $this->_mainPactModel->fetchRow("`mainVersion`='{$field['mainVersion']}'");
            $keepStartEventId = isset($mainPactRow['keepStartEventId'])? intval($mainPactRow['keepStartEventId']):0;
            $keepEndEventId = isset($mainPactRow['keepEndEventId'])? intval($mainPactRow['keepEndEventId']):0;
            if (!($field['eventId']>=$keepStartEventId&&$field['eventId']<=$keepEndEventId)) {
                echo $this->view->message('事件Id不在定义区间内,请重新填写。') ;
                exit;
            }
            if ($this->_mainPactEventModel->isExists($field['mainVersion'],$field['eventId'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]已经存在，请重新填写。") ;
                exit; 
            }
            
            if (strlen($field['eventBagName']) ==0) {
                echo $this->view->message('请输入事件别名。') ;
                exit;
            }
            if ($this->_mainPactEventModel->isExistsByEventBagName($field['mainVersion'],$field['eventBagName'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件别名[{$field['eventBagName']}]已经存在，请重新填写。") ;
                exit; 
            }
            
            //修改
            $num = $this->_mainPactEventModel->update($field, "`id` = {$id}");
            if ($num>0&&$row['eventId']!=$field['eventId']){
                //更新id字段
                $element = array();
                $element['eventId'] = $field['eventId'];
                $this->_mainPactEventFieldModel->update($element, "`mainVersion`='{$field['mainVersion']}' AND `eventId` = {$row['eventId']}");
                $this->_mainPactEventFieldEnumModel->update($element, "`mainVersion`='{$field['mainVersion']}' AND `eventId` = {$row['eventId']}");
                $this->_mainPactEventFieldLoopModel->update($element, "`mainVersion`='{$field['mainVersion']}' AND `eventId` = {$row['eventId']}");
            }
            
            
            $passwordMsg = ",协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function mainEventDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //清空字段
            foreach ($ids as $id){
                $row = $this->_mainPactEventModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_mainPactEventFieldModel->delete("`mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']}");
                    $this->_mainPactEventFieldEnumModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']}");
                    $this->_mainPactEventFieldLoopModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']}");
                }
            }
            //删除事件
            if (is_array($ids)) {
                $this->_mainPactEventModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
   /**
     * 主协议事件字段列表
     * 
     * @return void
     */
    public function mainEventFieldAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));

        $urls = $vars;
        if ($vars['mainVersion'] != '') {
            $wheres[] = "`mainVersion`= '{$vars['mainVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= '{$vars['eventId']}'";
        }
        $orderBy = ' sort ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT *,CONVERT(fieldSort,SIGNED) as sort FROM `{$this->_mainPactEventFieldModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getMainPactEventFieldSortIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $account = trim($this->_request->get('account'));
        $mainPactEventFieldId = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactEventFieldModel->isExists($mainVersion,$eventId,$account, $mainPactEventFieldId);
        }
        exit(Zend_Json::encode($isExists));
    }
   /**
     * 添加动作
     * 
     * @return void
     */
    public function mainEventFieldInsertAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //最大序号
        $maxSort = $this->_mainPactEventFieldModel->getMaxSort($vars['mainVersion'], $vars['eventId']);
        $this->view->maxSort = $maxSort+1;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['complyMode'] = (int)$filter->filter(trim($this->_request->getPost('complyMode')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['fieldSort']) ==0) {
                echo $this->view->message("请输入事件[{$field['eventId']}]的字段序号。") ;
                exit;
            }
            if ($this->_mainPactEventFieldModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'])) {
                echo $this->view->message("协议版本{$field['mainVersion']}事件[{$field['eventId']}]的字段序号已经存在，请重新填写。") ;
                exit; 
            }
            $id = $this->_mainPactEventFieldModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'main-event-field','mainVersion' => $field['mainVersion'],'eventId'=>$field['eventId']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function mainEventFieldUpdateAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactEventFieldModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['complyMode'] = (int)$filter->filter(trim($this->_request->getPost('complyMode')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            
            if (strlen($field['fieldSort']) ==0) {
                echo $this->view->message("请输入事件[{$field['eventId']}]的字段序号。") ;
                exit;
            }
            if ($this->_mainPactEventFieldModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]的字段序号[{$field['fieldSort']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $num = $this->_mainPactEventFieldModel->update($field, "`id` = {$id}");
            if ($num>0&&$row['fieldSort']!=$field['fieldSort']){
                //更新枚举类型
                $element = array();
                $element['fieldSort'] = $field['fieldSort'];
                $this->_mainPactEventFieldEnumModel->update($element, "`mainVersion`='{$row['mainVersion']}' AND `eventId` = {$row['eventId']} AND `fieldSort` = {$row['fieldSort']}");
                $this->_mainPactEventFieldLoopModel->update($element, "`mainVersion`='{$row['mainVersion']}' AND `eventId` = {$row['eventId']} AND `fieldSort` = {$row['fieldSort']}");
            }
            $passwordMsg = ",协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
    
   /**
     * 删除动作
     * 
     * @return void
     */
    public function mainEventFieldDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            foreach ($ids as $id){
                $row = $this->_mainPactEventFieldModel->fetchRow("`id` = {$id}");
                if ($row){
                    $this->_mainPactEventFieldEnumModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']} AND `fieldSort` ={$row['fieldSort']}");
                    $this->_mainPactEventFieldLoopModel->delete(" `mainVersion`='{$row['mainVersion']}' AND `eventId`={$row['eventId']} AND `fieldSort` ={$row['fieldSort']}");
                }
            }
            if (is_array($ids)) {
                $this->_mainPactEventFieldModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
   /**
     * 主协议Id定义列表
     * 
     * @return void
     */
    public function mainIdDefineAction()
    {
        //获取当前页码
        $pageSize = 2000;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'))? trim($this->_request->get('fieldType')):'C';

        $urls = $vars;
        if ($vars['mainVersion'] != '') {
            $wheres[] = "`mainVersion`= '{$vars['mainVersion']}'";
        }
        if ($vars['fieldType'] != '') {
            $wheres[] = "`fieldType`= '{$vars['fieldType']}'";
        }
        $orderBy = '`fieldType` DESC,`pageId` ASC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_mainPactIdDefineModel->getTableName()}`{$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getMainPactPageIdIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $fieldType = trim($this->_request->get('fieldType'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactIdDefineModel->isExists($mainVersion,$fieldType,$account, $id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function mainIdDefineInsertAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = (int)trim($this->_request->getPost('pageId'));
            $field['pageName'] = $filter->filter(trim($this->_request->getPost('pageName')));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            if($field['relationId']==''){
                $field['relationId'] = $field['pageId'];
            }
            $field['aidNote'] = $filter->filter(trim($this->_request->getPost('aidNote')));
            $field['isUse'] = $this->_request->getPost('isUse');
            if (!$field['isUse']){
                $field['isUse']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入Id值。') ;
                exit;
            }
            if ($this->_mainPactIdDefineModel->isExists($field['mainVersion'],$field['pageId'],$field['pageId'])) {
                echo $this->view->message('此协议版本此Id值已经存在，请重新填写。') ;
                exit; 
            }
            $id = $this->_mainPactIdDefineModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'main-id-define','mainVersion' => $field['mainVersion'],'fieldType'=>$field['fieldType']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function mainIdDefineUpdateAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['fieldType'] = trim($this->_request->get('fieldType'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactIdDefineModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['fieldType'] = trim($this->_request->getPost('fieldType'));
            $field['pageId'] = (int)trim($this->_request->getPost('pageId'));
            $field['pageName'] = $filter->filter(trim($this->_request->getPost('pageName')));
            $field['relationId'] = $filter->filter(trim($this->_request->getPost('relationId')));
            $field['aidNote'] = $filter->filter(trim($this->_request->getPost('aidNote')));
            $field['isUse'] = $this->_request->getPost('isUse');
            if (!$field['isUse']){
                $field['isUse']=0;
            }
            $field['isUseBi'] = $this->_request->getPost('isUseBi');
            if (!$field['isUseBi']){
                $field['isUseBi']=0;
            }
            $field['isFlag'] = $this->_request->getPost('isFlag');
            if (!$field['isFlag']){
                $field['isFlag']=0;
            }
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));

            if (strlen($field['pageId']) ==0) {
                echo $this->view->message('请输入Id值。') ;
                exit;
            }
            if ($this->_mainPactIdDefineModel->isExists($field['mainVersion'],$field['pageId'],$field['pageId'], $row['id'])) {
                echo $this->view->message("协议版本[{$field['mainVersion']}]Id定义值[{$field['pageId']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $this->_mainPactIdDefineModel->update($field, "`id` = {$id}");
            $passwordMsg = ",协议版本[{$field['mainVersion']}]Id定义值[{$field['pageId']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function mainIdDefineDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //删除id值
            if (is_array($ids)) {
                $this->_mainPactIdDefineModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    /**
     * 字段类型
     * 
     * @return void
     */
    public function fieldTypeAction()
    {
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
    
   
    
    
   /**
     * 主协议事件枚举类型
     * 
     * @return void
     */
    public function mainFieldEnumAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        
        $urls = $vars;
        if ($vars['mainVersion'] != '') {
            $wheres[] = "`mainVersion`= '{$vars['mainVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= {$vars['eventId']}";
        }
        if ($vars['fieldSort'] != '') {
            $wheres[] = "`fieldSort`= {$vars['fieldSort']}";
        }
        $orderBy = '`enum` ASC';

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_mainPactEventFieldEnumModel->getTableName()}`{$where} ORDER BY {$orderBy}";
        $rows = $this->_mainPactEventFieldEnumModel->getAdapter()->fetchAll($sql);

        $this->view->rows = $rows;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getEventFieldEnumIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $fieldSort = trim($this->_request->get('fieldSort'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactEventFieldEnumModel->isExists($mainVersion,$eventId,$fieldSort,$account,$id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
   /**
     * 添加动作
     * 
     * @return void
     */
    public function mainFieldEnumInsertAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        $this->view->enumAuthor= Application_Model_UserLogin::getInstance()->getLoginedUserName();
        
        $sql = "SELECT * FROM `{$this->_mainPactEventFieldModel->getTableName()}` WHERE `mainVersion`='{$vars['mainVersion']}' AND `eventId`={$vars['eventId']} AND `fieldSort`={$vars['fieldSort']}";
        $eventFieldRow = $this->_mainPactEventFieldModel->getAdapter()->fetchRow($sql);
        $fieldType = $eventFieldRow['fieldType'];
        $this->view->fieldType = $fieldType;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['enum'] = (int)$filter->filter(trim($this->_request->getPost('enum')));
            $field['enumValue'] = $filter->filter(trim($this->_request->getPost('enumValue')));
            $field['enumAuthor'] = $filter->filter(trim($this->_request->getPost('enumAuthor')));
            $field['addTime']= time();
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['mainVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            
            
            $field['fieldType'] = $fieldType; 
            if (in_array($field['fieldType'],array('E','N'))){
	            if (!($field['enum']>=0&&$field['enum']<=127)){
	                echo $this->view->message("枚举键范围[0-127],请重新输入!") ;
	                exit;
	            }
            }
            if (in_array($field['fieldType'],array('F','G'))){
	            if (!($field['enum']>=0&&$field['enum']<=32767)){
	                echo $this->view->message("枚举键范围[0-32767],请重新输入!") ;
	                exit;
	            }
            }
            if ($this->_mainPactEventFieldEnumModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'],$field['enum'])) {
                echo $this->view->message("协议版本{$field['mainVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的枚举键已经存在，请重新填写。") ;
                exit; 
            }
            
            $id = $this->_mainPactEventFieldEnumModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'main-field-enum','mainVersion' => $field['mainVersion'],'eventId'=>$field['eventId'],'fieldSort'=>$field['fieldSort']))) ;
            exit;
        }
    }
    
   /**
     * 更新动作
     * 
     * @return void
     */
    public function mainFieldEnumUpdateAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactEventFieldEnumModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        
        $sql = "SELECT * FROM `{$this->_mainPactEventFieldModel->getTableName()}` WHERE `mainVersion`='{$vars['mainVersion']}' AND `eventId`={$vars['eventId']} AND `fieldSort`={$vars['fieldSort']}";
        $eventFieldRow = $this->_mainPactEventFieldModel->getAdapter()->fetchRow($sql);
        $fieldType = $eventFieldRow['fieldType'];
        $this->view->fieldType = $fieldType;
       
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['enum'] = (int)$filter->filter(trim($this->_request->getPost('enum')));
            $field['enumValue'] = $filter->filter(trim($this->_request->getPost('enumValue')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            $field['fieldType'] = $row['fieldType'];
            
            if (strlen($field['mainVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            if (!$row['fieldType']){
                $field['fieldType'] = $fieldType; 
            }
            if (in_array($field['fieldType'],array('E','N'))){
	            if (!($field['enum']>=0&&$field['enum']<=127)){
	                echo $this->view->message("枚举键范围[0-127],请重新输入!") ;
	                exit;
	            }
            }
            if (in_array($field['fieldType'],array('F','G'))){
	            if (!($field['enum']>=0&&$field['enum']<=32767)){
	                echo $this->view->message("枚举键范围[0-32767],请重新输入!") ;
	                exit;
	            }
            }
            if ($this->_mainPactEventFieldEnumModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'],$field['enum'], $row['id'])) {
                echo $this->view->message("协议版本{$field['mainVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的枚举键已经存在，请重新填写。") ;
                exit; 
            }
            
            //修改
            $num = $this->_mainPactEventFieldEnumModel->update($field, "`id` = {$id}");
            if ($num>0&&$row['fieldType']=='E'){
                //修改全局
                $element = array();
                $element['enum'] = $field['enum'];
                $element['enumValue'] = $field['enumValue'];
                $element['remark'] = $field['remark'];
                $childPactRows = $this->_childPactModel->fetchAll("`mainVersion`='{$row['mainVersion']}'");
                foreach ($childPactRows as $childRow){
                    $this->_childPactEventFieldEnumModel->update($element, "`childVersion`='{$childRow['childVersion']}' AND `eventId`={$row['eventId']} AND `fieldSort`={$row['fieldSort']} AND `enum`={$row['enum']}  AND  `fieldType` = '{$row['fieldType']}'");
                }
            }
            
            
            $passwordMsg = ",协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]枚举信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
   /**
     * 删除动作
     * 
     * @return void
     */
    public function mainFieldEnumDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            //删除id值
            if (is_array($ids)) {
                $this->_mainPactEventFieldEnumModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
    
    /**
     * 主协议事件循环类型
     * 
     * @return void
     */
    public function mainFieldLoopAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        
        $urls = $vars;
        if ($vars['mainVersion'] != '') {
            $wheres[] = "`mainVersion`= '{$vars['mainVersion']}'";
        }
        if ($vars['eventId'] != '') {
            $wheres[] = "`eventId`= {$vars['eventId']}";
        }
        if ($vars['fieldSort'] != '') {
            $wheres[] = "`fieldSort`= {$vars['fieldSort']}";
        }
        $orderBy = '`loopFieldSort` ASC';

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_mainPactEventFieldLoopModel->getTableName()}`{$where} ORDER BY {$orderBy}";
        $rows = $this->_mainPactEventFieldLoopModel->getAdapter()->fetchAll($sql);

        $this->view->rows = $rows;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
    }
    
   /**
     * 检测是否存在
     * 
     * @return void
     */
    public function getEventFieldLoopIsExistsByJsonAction()
    {
        $mainVersion = trim($this->_request->get('mainVersion'));
        $eventId = trim($this->_request->get('eventId'));
        $fieldSort = trim($this->_request->get('fieldSort'));
        $account = trim($this->_request->get('account'));
        $id = intval($this->_request->get('id'));
        $isExists = 1;
        if ($account != '') {
            $isExists = $this->_mainPactEventFieldLoopModel->isExists($mainVersion,$eventId,$fieldSort,$account,$id);
        }
        exit(Zend_Json::encode($isExists));
    }
    
    
    /**
     * 添加动作
     * 
     * @return void
     */
    public function mainFieldLoopInsertAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        //最大序号
        $maxSort = $this->_mainPactEventFieldLoopModel->getMaxSort($vars['mainVersion'], $vars['eventId'],$vars['fieldSort']);
        $this->view->maxSort = $maxSort+1;
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['loopFieldSort'] = (int)$filter->filter(trim($this->_request->getPost('loopFieldSort')));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['complyMode'] = $filter->filter(trim($this->_request->getPost('complyMode')));
            $field['addTime']= time();
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));

            if (strlen($field['mainVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            if (!($field['loopFieldSort']>0)){
                echo $this->view->message("字段序号有误,请重新输入!") ;
                exit;
            }
            if ($this->_mainPactEventFieldLoopModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'],$field['loopFieldSort'])) {
                echo $this->view->message("协议版本{$field['mainVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的字段序号已经存在，请重新填写。") ;
                exit; 
            }
            $id = $this->_mainPactEventFieldLoopModel->insert($field);

            echo $this->view->message('操作成功。', $this->view->projectUrl(array('action' => 'main-field-loop','mainVersion' => $field['mainVersion'],'eventId'=>$field['eventId'],'fieldSort'=>$field['fieldSort']))) ;
            exit;
        }
    }
    
    /**
     * 更新动作
     * 
     * @return void
     */
    public function mainFieldLoopUpdateAction()
    {
        $vars = array();
        $vars['mainVersion'] = trim($this->_request->get('mainVersion'));
        $vars['eventId'] = trim($this->_request->get('eventId'));
        $vars['fieldSort'] = trim($this->_request->get('fieldSort'));
        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $this->view->vars = $vars;
        
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit("没有传递参数ID");
        }
        $row = $this->_mainPactEventFieldLoopModel->fetchRow("`id` = {$id}"); 
        if (empty($row)) {
             exit("没有找到记录。");
        }
        $this->view->row = $row;
        
        //字段类型
        $fieldTypeRows = $this->_fieldTypeModel->getKeyValues();
        $this->view->fieldTypeRows = $fieldTypeRows;
        
        if ($this->_request->isPost()) {
            $field = array();
            $filter = new Zend_Filter_StripTags();
            $field['mainVersion'] = $filter->filter(trim($this->_request->getPost('mainVersion')));
            $field['eventId'] = (int)trim($this->_request->getPost('eventId'));
            $field['fieldSort'] = (int)$filter->filter(trim($this->_request->getPost('fieldSort')));
            $field['loopFieldSort'] = (int)$filter->filter(trim($this->_request->getPost('loopFieldSort')));
            $field['fieldName'] = $filter->filter(trim($this->_request->getPost('fieldName')));
            $field['fieldType'] = $filter->filter(trim($this->_request->getPost('fieldType')));
            $field['complyMode'] = $filter->filter(trim($this->_request->getPost('complyMode')));
            $field['note'] = $filter->filter(trim($this->_request->getPost('note')));
            $field['remark'] = $filter->filter(trim($this->_request->getPost('remark')));
            
            if (strlen($field['mainVersion']) ==0||strlen($field['eventId']) ==0||strlen($field['fieldSort']) ==0) {
                echo $this->view->message("非法操作!") ;
                exit;
            }
            if (!($field['loopFieldSort']>0)){
                echo $this->view->message("字段序号有误,请重新输入!") ;
                exit;
            }
            if ($this->_mainPactEventFieldLoopModel->isExists($field['mainVersion'],$field['eventId'],$field['fieldSort'],$field['loopFieldSort'], $row['id'])) {
                echo $this->view->message("协议版本{$field['mainVersion']}事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]的循环的字段序号[{$field['loopFieldSort']}]已经存在，请重新填写。") ;
                exit; 
            }
            //修改
            $this->_mainPactEventFieldLoopModel->update($field, "`id` = {$id}");
            
            $passwordMsg = ",协议版本[{$field['mainVersion']}]事件[{$field['eventId']}]字段序号[{$field['fieldSort']}]循环的字段序号[{$field['loopFieldSort']}]信息已更新";

            $backUrl = $this->_request->get('backUrl') != NULL ? urldecode($this->_request->get('backUrl')) : @$_SERVER['HTTP_REFERER'];
            echo $this->view->message("操作成功{$passwordMsg}。", $backUrl) ;
            exit;
        }
    }
    
    /**
     * 删除动作
     * 
     * @return void
     */
    public function mainFieldLoopDeleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_mainPactEventFieldLoopModel->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
    
    
    
    
    
    
    
}